package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealService;
import javassist.bytecode.LineNumberAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/medicalIncome")
@RestController
public class MedicalIncomeController {
    @Reference
    private SetmealService setmealService;
    @Reference
    private OrderService orderService;

    @RequestMapping("/findData")
    public Result findDate(@RequestBody String[] date) {
        Map map = new HashMap();
        map.put("beginDate", date[0]);
        map.put("endDate", date[1]);
        List<Map<String, Object>> setmeals = setmealService.findData(map);
        if (setmeals.size() != 0 && setmeals != null) {
            Map<String, Object> map1 = new HashMap<>();
            //创建name集合
            List<Object> names = new ArrayList<>();
            //创建收入集合
            List<Map<String, Object>> incomes = new ArrayList<>();
            for (Map<String, Object> setmeal : setmeals) {
                Map<String, Object> map2 = new HashMap<>();
                Object name = setmeal.get("name");
                names.add(name);
                Object income = setmeal.get("income");
                map2.put("name", name);
                map2.put("value", income);
                incomes.add(map2);
            }
            //求总收入
            Double allIncome = 0.0;
            for (Map<String, Object> income : incomes) {
                allIncome += Float.parseFloat(String.valueOf(income.get("value")));
            }
            map1.put("names", names);
            map1.put("incomes", incomes);
            map1.put("allIncome", allIncome);
            return new Result(true, "查询体检收入成功", map1);
        } else {
            //未查询到数据
            return new Result(false, "未查询到数据");
        }

    }
}
